﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PBO.Network.FourPlayer
{
    public class FourPlayerClient : IFourPlayerClientService
    {
        private ITopClient client;

        public FourPlayerClient(string ip)
        {
            this.client = new BattleClient(ip, (int)ServerPort.FourPlayerServerPort);
            this.client.ReceiveData += OnReceiveData;
        }

        public FourPlayerClient(ITopClient client)
        {
            this.client = client;
            this.client.ReceiveData += OnReceiveData;
        }

        #region IFourPlayerClientService

        public event Action<byte, string> OnPositionSet;
        public event Action<int> OnBattleReady;
        public event Action<byte> OnMyPositionSet;
        public event Action OnServerClosed;

        public void SetPosition(byte position, string player)
        {
            var rsp = new PositionResponse() { position = position, player = player };
            client.SendData(EnumFourPlayerMethods.Server_SetPosition_Hash, rsp);
        }

        public void StartBattle()
        {
            client.SendData(EnumFourPlayerMethods.Server_StartBattle_Hash, string.Empty);
        }

        public void Close()
        {
            client.SendData(EnumFourPlayerMethods.Server_Close_Hash, string.Empty);
        }

        public void Logon(int identity)
        {
            client.SendData(EnumFourPlayerMethods.Server_Logon_Hash, identity);
        }

        #endregion

        #region event

        protected void OnSetPosition(byte position, string player)
        {
            if (OnPositionSet != null) OnPositionSet(position, player);
        }

        protected void OnStartBattle(int identity)
        {
            if (OnBattleReady != null) OnBattleReady(identity);
        }

        protected void OnSetPositionSuccess(byte position)
        {
            if (OnMyPositionSet != null) OnMyPositionSet(position);
        }

        protected void OnClose()
        {
            if (OnServerClosed != null) OnServerClosed();
        }

        #endregion

        protected void OnReceiveData(int sessionID, int method, string json)
        {
            switch (method)
            {
                case EnumFourPlayerMethods.Client_SetPosition_Hash:
                    var rsp = StringUtils.ParseResponse<PositionResponse>(json);
                    OnSetPosition(rsp.position, rsp.player);
                    break;
                case EnumFourPlayerMethods.Client_SetPositionSuccess_Hash:
                    OnSetPositionSuccess(byte.Parse(json));
                    break;
                case EnumFourPlayerMethods.Client_StartBattle_Hash:
                    OnStartBattle(int.Parse(json));
                    break;
                case EnumFourPlayerMethods.Client_Close_Hash:
                    OnClose();
                    break;
            }
        }
    }
}
